﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ExameRegister.Views;

namespace ExameRegister.Assert
{
    public partial class NavMoveSwitch : UserControl
    {
        private int _tail = 0;//0、1
        private int _index = 0;

        public NavMoveSwitch()
        {
            InitializeComponent();
        }

        public FrameworkElement FirstPage
        {
            get { return placeHolder1.Children[0] as FrameworkElement; }
            set { placeHolder1.Children.Clear(); placeHolder1.Children.Add(value); }
        }

        public IAsyncCtrl MiddlePage
        {
            get 
            {
                if (placeHolder2.Children.Count == 0)
                {
                    return null;
                }
                return placeHolder2.Children[0] as IAsyncCtrl;
            }
            set 
            { 
                placeHolder2.Children.Clear(); 
                placeHolder2.Children.Add((UIElement)value); 
            }
        }

        public IAsyncCtrl LastPage
        {
            get 
            {
                if (placeHolder3.Children.Count == 0)
                {
                    return null;
                }
                return placeHolder3.Children[0] as IAsyncCtrl;
            }
            set 
            { 
                placeHolder3.Children.Clear(); 
                placeHolder3.Children.Add((UIElement)value); 
            }
        }
        public IAsyncCtrl LastPage1
        {
            get
            {
                if (placeHolder4.Children.Count == 0)
                {
                    return null;
                }
                return placeHolder4.Children[0] as IAsyncCtrl;
            }
            set
            {
                placeHolder4.Children.Clear();
                placeHolder4.Children.Add((UIElement)value);
            }
        }
        public IAsyncCtrl LastPage11
        {
            get
            {
                if (placeHolder5.Children.Count == 0)
                {
                    return null;
                }
                return placeHolder5.Children[0] as IAsyncCtrl;
            }
            set
            {
                placeHolder5.Children.Clear();
                placeHolder5.Children.Add((UIElement)value);
            }
        }
        public void SetNextPage(IAsyncCtrl ctrl)
        {
            switch(_index)
            {
                case 0:
                    MiddlePage = ctrl;
                    break;
                case 1:
                    LastPage = ctrl;
                    break;
                case 2:
                    LastPage1 = ctrl;
                    break;
                case 3:
                    LastPage11 = ctrl;
                    break;
                default:
                    throw new Exception("已经是最后一页了，只支持5个页");
            }
        }

        public void MoveNext()
        {
            if (_index == 4)
            {
                throw new Exception("不能再后移了，到最后了。");
            }

            _index++;

            if (_index == 1)
            {
                MoveLeftToMiddlePage();
            }
            else if (_index == 2)
            {
                MoveLeftToLastPage();
            }
            else if (_index == 3)
            {

                MoveLeftToLastPage1();
            }
            else
            {
                MoveLeftToLastPage11();
            }
        }

        public void MovePrev()
        {
            if (_index == 0)
            {
                throw new Exception("不能再前移了，到第一个了。");
            }

            _index--;
            if (_index == 0)
            {
                MoveRightToFirstPage();
            }
            else if (_index == 1)
            {
                MoveRightToMiddlePage();
            }
            else if(_index == 2)
            {
                MoveRightToFirstPage1();
            }
            else
            {
                 MoveRightToFirstPage11();
            }
        }

        private void MoveLeft(DoubleAnimation prev, DoubleAnimation next)
        {
            prev.To = -LayoutRoot.ActualWidth;
            next.From = LayoutRoot.ActualWidth + 4;
        }

        private void MoveRight(DoubleAnimation prev, DoubleAnimation next)
        {
            prev.From = -LayoutRoot.ActualWidth - 4;
            next.To = LayoutRoot.ActualWidth;
        }

        /// <summary>
        /// 1
        /// </summary>
        private void MoveLeftToMiddlePage()
        {
            MoveLeft(daPrev_LeftToMiddle, daNext_LeftToMiddle);
            sbMoveLeftToMiddle.Begin();
            placeHolder2.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// 2
        /// </summary>
        private void MoveLeftToLastPage()
        {
            MoveLeft(daPrev_LeftToLast, daNext_LeftToLast);
            sbMoveLeftToLast.Begin();
            placeHolder3.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// 3
        /// </summary>
        private void MoveLeftToLastPage1()
        {
            MoveLeft(daPrev_LeftToLast1, daNext_LeftToLast1);
            sbMoveLeftToLast1.Begin();
            placeHolder4.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// 4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void MoveLeftToLastPage11()
        {
            MoveLeft(daPrev_LeftToLast11, daNext_LeftToLast11);
            sbMoveLeftToLast11.Begin();
            placeHolder5.Visibility = Visibility.Visible;
        }
        private void MoveRightToFirstPage()
        {
            MoveRight(daPrev_RightToFirst, daNext_RightToFirst);
            sbMoveRightToFirst.Begin();
            placeHolder1.Visibility = Visibility.Visible;
        }

        private void MoveRightToMiddlePage()
        {
            MoveRight(daPrev_RightToMiddle, daNext_RightToMiddle);
            sbMoveRightToMiddle.Begin();
            placeHolder2.Visibility = Visibility.Visible;
        }
        private void MoveRightToFirstPage1()
        {
            MoveRight(daPrev_RightToFirst1, daNext_RightToFirst1);
            sbMoveRightToFirst1.Begin();
            placeHolder3.Visibility = Visibility.Visible;
        }

        private void MoveRightToFirstPage11()
        {
            MoveRight(daPrev_RightToFirst11, daNext_RightToFirst11);
            sbMoveRightToFirst11.Begin();
            placeHolder4.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// 1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveLeftToMiddle_Completed(object sender, EventArgs e)
        {
            placeHolder1.Visibility = Visibility.Collapsed;
            MiddlePage.PageLoaded = true;
        }
        /// <summary>
        /// 2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveLeftToLast_Completed(object sender, EventArgs e)
        {
            placeHolder2.Visibility = Visibility.Collapsed;
            LastPage.PageLoaded = true;
        }
        /// <summary>
        /// 3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveLeftToLast1_Completed(object sender, EventArgs e)
        {
            placeHolder3.Visibility = Visibility.Collapsed;
            LastPage1.PageLoaded = true;
        }
        /// <summary>
        /// 4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveLeftToLast11_Completed(object sender, EventArgs e)
        {
            placeHolder4.Visibility = Visibility.Collapsed;
            LastPage11.PageLoaded = true;
        }
        /// <summary>
        /// s1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveRightToFirst_Completed(object sender, EventArgs e)
        {
            placeHolder2.Visibility = Visibility.Collapsed;
        }
        /// <summary>
        /// s2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveRightToMiddle_Completed(object sender, EventArgs e)
        {
            placeHolder3.Visibility = Visibility.Collapsed;
        }
        /// <summary>
        /// s3
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveRightToFirst1_Completed(object sender, EventArgs e)
        {
            placeHolder4.Visibility = Visibility.Collapsed;
        }
        /// <summary>
        /// s4
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbMoveRightToFirst11_Completed(object sender, EventArgs e)
        {
            placeHolder5.Visibility = Visibility.Collapsed;
        }

        private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Height <= 0 || e.NewSize.Width <= 0)
            {
                return;
            }

            LayoutRoot.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, e.NewSize.Width, e.NewSize.Height) };
        }

      
       
        


        //public static ReturnAsyncCtrl ShowNextPage(this NavMoveSwitch nav, string title, AsyncCtrl ctrl, IReturnListenerc prevPage)
        //{
        //    ctrl.Nav = nav;
        //    ReturnAsyncCtrl page = new ReturnAsyncCtrl(ctrl) { Nav = nav, Title = title, PrevPage = prevPage };
        //    nav.SetNextPage(page);
        //    nav.MoveNext();
        //    return page;
        //}
    }

    

}
